package com.kingschan.jeasytag.tag.grid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;

import org.json.JSONException;
import org.json.JSONObject;

import com.kingschan.jeasytag.core.annotations.tag.*;
import com.kingschan.jeasytag.container.EasyTagContainer;
import com.kingschan.jeasytag.container.impl.EasyTagContainerImpl;
import com.kingschan.jeasytag.tag.RootTag;
import com.kingschan.jeasytag.tag.power.ExportTag;
import com.kingschan.jeasytag.tag.power.SQLTag;
import com.kingschan.jeasytag.tag.power.SearchBoxTag;
import com.kingschan.util.lang.StringUtil;
/**
 * 数据表格控件
 * @author kingschan
 *2013-4-6
 */
@TagMetaData(CssClass = "easyui-datagrid", tag = "table"		
)
public class GridTag extends RootTag {
	//private static Logger log = Logger.getLogger(GridTag.class);
	private static final long serialVersionUID = 1L;
	//数据表格列配置对象，查看列属性以获取更多细节。
	private List<ColumnTag> columns;
	//跟列属性一样，但是这些列固定在左边，不会滚动。
	private List<ColumnTag> frozenColumns;
	//sql语句标签
	private SQLTag sqltag;
	//导出的能力
	private ExportTag export;
	//查询框的能力
	private SearchBoxTag search;
	//多选框的columns
	private List<ColumnTag> checkBoxColumns;
	//html 代码
	private StringBuffer html=null;
	private EasyTagContainer container=EasyTagContainerImpl.getInstance();
	private Boolean autotoolbar;
	
	@NumberField(description="高度")
	private String height;
	@NumberField(description="宽度")
	private String width;
	@StringField(description="grid标题")
	private String title;
	@BooleanField(description="设置为true将自动使列适应表格宽度以防止出现水平滚动")
	private String fitColumns;
	@BooleanField(description="定义设置行的高度，根据该行的内容。设置为false可以提高负载性能")	
	private String  autoRowHeight;
	@StringField(appendFlag="'",description="通过数组定义工具栏")
	private String toolbar;
	@BooleanField(description="设置为true将交替显示行背景。")
	private String striped;
	@BooleanField(description="设置为true，当数据长度超出列宽时将会自动截取")
	private String nowrap;	
	@StringField(description="表明该列是一个唯一列")
	private String idField;
	@StringField(description="一个用以从远程站点请求数据的超链接地址")
	private String url;
	@StringField(description="当从远程站点载入数据时，显示的一条快捷信息")
	private String loadMsg;
	@BooleanField(description="设置true将在数据表格底部显示分页工具栏")
	private String pagination;
	@BooleanField(description="设置为true将显示行数")
	private String rownumbers;
	@BooleanField(description="设置为true将只允许选择一行")
	private String singleSelect;
	@BooleanField(description="如果为true，该复选框被选中/取消选中，当用户点击某一行上")
	private String checkOnSelect;
	@BooleanField(description="如果设置为true，单击一个复选框，将始终选择行如果为false")
	private String selectOnCheck;
	@StringField(description="定义的分页栏的位置.可用的值有 'top','bottom','both'")
	private String pagePosition;
	@NumberField(description="当设置分页属性时，初始化分页码")
	private String pageNumber;
	@NumberField(description="当设置分页属性时，初始化每页记录数")
	private String pageSize;
	@StringField(description="当设置分页属性时，初始化每页记录数列表[10,20,30,40,50]",appendFlag="")
	private String pageList;
	@JsonField(description="当请求远程数据时，发送的额外参数")
	private String queryParams;
	@StringField(description="当数据表格初始化时以哪一列来排序")
	private String sortName;
	@StringField(description="定义排序顺序，可以是'asc'或者'desc'正序或者倒序")
	private String sortOrder;
	@BooleanField(description="定义是否通过远程服务器对数据排序")
	private String remoteSort;
	@BooleanField(description="定义是否显示行底（如果是做统计表格，这里可以显示总计等）。")
	private String showFooter;
	@EventField(args={"index","row"},description="返回样式，如：'background:red'，function有2个参数：index：行索引，从0开始.row：对应于该行记录的对象")
	private String rowStyler;
	@EventField(args="data",description="返回过滤的数据显示。该函数需要一个参数'data'，表示原始数据。您可以更改源数据的标准数据格式。此函数必须返回标准数据对象中包含的“total”和“rows”的属性。")
	private String loadFilter;
	@EventField(args="data",description="当数据载入成功时触发")
	private String onLoadSuccess;
	@EventField(args="",description="当载入远程数据发生错误时触发")
	private String onLoadError;
	@EventField(args="param",description="在请求载入数据之前触发，如果返回false将取消载入")
	private String onBeforeLoad;
	@EventField(args={"rowIndex","rowData"},description="当用户点击行时触发，参数如下：rowIndex：被点击的行索引，从0开始。rowData：对应于被点击的行的记录。")
	private String onClickRow;	
	@EventField(args={"rowIndex","rowData"},description="当用户双击一行时触发，参数如下：rowIndex：被点击的行索引，从0开始。rowData：对应于被点击的行的记录")
	private String onDblClickRow;
	@EventField(args={"rowIndex","field"},description="当用户点击单元格时触发")
	private String onClickCell;
	@EventField(args={"rowIndex","field"},description="当用户双击单元格时触发")
	private String onDblClickCell;
	@EventField(args={"sort","order"},description="当用户对列排序时触发，参数如下：sort：排序字段名称order：排序顺序")
	private String 	onSortColumn;
	@EventField(args={"field", "width"},description="当用户调整列宽时触发")
	private String 	onResizeColumn;	
	@EventField(args={"rowIndex","rowData"},description="当用户选择一行是触发，参数如下：rowIndex：被选择的行索引，从0开始。rowData：对应于被选择行的记录")
	private String 	onSelect;
	@EventField(args={"rowIndex","rowData"},description="当用户取消选择一行时触发，参数如下：rowIndex：被取消选择的行索引，从0开始。rowData：对应于被取消选择行的记录")
	private String 	onUnselect;
	@EventField(args={"rows"},description="当用户选择所有行时触发")
	private String 	onSelectAll;
	@EventField(args={"rows"},description="当用户取消选择所有行时触发")
	private String 	onUnselectAll;
	@EventField(args={"rowIndex","rowData"},description="当用户选中行时触发，参数包含：rowIndex：选中行的索引，从0开始rowData：选中的行对应的记录")
	private String 	onCheck;
	@EventField(args={"rowIndex","rowData"},description="当用户取消选中行时触发，参数包含：rowIndex：取消选中行的索引，从0开始rowData：未经检查的行对应的记录")
	private String 	onUncheck;
	@EventField(args={"rows"},description="当用户检查所有行时触发")
	private String 	onCheckAll;
	@EventField(args={"rows"},description="用户取消所有行时触发")
	private String 	onUncheckAll;
	@EventField(args={"rowIndex","rowData"},description="当用户开始编辑一行时触发，参数如下：rowIndex：正在编辑的行索引，从0开始。rowData：对应于正在编辑的行的记录")
	private String 	onBeforeEdit;
	@EventField(args={"rowIndex","rowData","changes"},description="当用户编辑完成时触发，参数如下：rowIndex：正在编辑的行索引，从0开始。rowData：对应于正在编辑的行的记录。changes：被改变的字段内容，对应方式为字段：值。")
	private String 	onAfterEdit;
	@EventField(args={"rowIndex","rowData"},description="当用户取消编辑行时触发，参数如下：rowIndex：正在编辑的行索引，从0开始。rowData：对应于正在编辑的行的记录")
	private String 	onCancelEdit;
	@EventField(args={"e","field"},description="当数据表格的列标题被鼠标右键单击时触发。")
	private String 	onHeaderContextMenu;
	@EventField(args={"e","rowIndex","rowData"},description="当一行被鼠标右键单击时触发")
	private String 	onRowContextMenu;
	@StringField(description="定义数据表格的视图",appendFlag="")
	private String view;

	/**
	 * add query params
	 * @param key
	 * @param value
	 */
	public void addQueryParams(String key,String value){
		JSONObject args = null;
		try {
			args=null==this.getQueryParams()?new JSONObject():new JSONObject(this.getQueryParams());
			args.put(key, value);
			this.setQueryParams(args.toString());
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
	}
	
	/**
	 * add frozenColumns into grid
	 * @param ct
	 */
	public void addFrozenColumns(ColumnTag ct){
		this.frozenColumns.add(ct);
	}
	
	public void addHtml(String str){
		this.html.append(str);
	}
	
	@Override
	public int doStartTag() throws JspException {
		this.columns=new ArrayList<ColumnTag>();
		this.frozenColumns= new ArrayList<ColumnTag>();
		this.checkBoxColumns=new ArrayList<ColumnTag>();
		this.html= new StringBuffer();	
		this.autotoolbar=false;
		return super.doStartTag();
	}
	
	


	@Override
	public int doEndTag() throws JspException {		
		GridAutoAnalysis gal = new GridAutoAnalysis(this);
	try {
		gal.analysis();
		this.pageContext.getOut().write(html.toString());
		getSuport().appendSupport(GridTag.class);
	} catch (IOException e) {		
		e.printStackTrace();
	} catch (Exception e) {
		e.printStackTrace();
	}
	
		return super.doEndTag();
	}
	
	/**
	 * 得到pageContext
	 * @return
	 */
	public PageContext getPagecontent(){
		return this.pageContext;
	}
	
	
	
	/**
	 * 添加普通列
	 * @param tag
	 */
	public void addColumn(ColumnTag tag){
		if (StringUtil.ConvertNullStr(tag.getCheckbox()).equals("true")) {
			this.checkBoxColumns.add(tag);
		}		
		columns.add(tag);

	}
	/**
	 * 得到第一个为多选框的字段
	 * @return  字段的field值
	 */
	public String getCheckboxField(){
		String fieldName=null;
		if (null!=this.checkBoxColumns) {
			for (ColumnTag c : checkBoxColumns) {
				if (null!=c.getCheckbox()&&c.getCheckbox().matches("[true]{4}")) {
					fieldName=c.getField();
					break;
				}
			}
		}
		return fieldName;
	}

	public List<ColumnTag> getColumns() {
		return columns;
	}


	public void setColumns(List<ColumnTag> columns) {
		this.columns = columns;
	}



	public String getFitColumns() {
		return fitColumns;
	}


	public void setFitColumns(String fitColumns) {
		this.fitColumns = fitColumns;
	}


	public String getAutoRowHeight() {
		return autoRowHeight;
	}


	public void setAutoRowHeight(String autoRowHeight) {
		this.autoRowHeight = autoRowHeight;
	}


	public String getToolbar() {
		return toolbar;
	}


	public void setToolbar(String toolbar) {
		this.toolbar = toolbar;
	}


	public String getStriped() {
		return striped;
	}


	public void setStriped(String striped) {
		this.striped = striped;
	}


	public String getNowrap() {
		return nowrap;
	}


	public void setNowrap(String nowrap) {
		this.nowrap = nowrap;
	}


	public String getIdField() {
		return idField;
	}


	public void setIdField(String idField) {
		this.idField = idField;
	}


	public String getUrl() {
		return url;
	}


	public void setUrl(String url) {
		this.url = url;
	}


	public String getLoadMsg() {
		return loadMsg;
	}


	public void setLoadMsg(String loadMsg) {
		this.loadMsg = loadMsg;
	}


	public String getPagination() {
		return pagination;
	}


	public void setPagination(String pagination) {
		this.pagination = pagination;
	}


	public String getRownumbers() {
		return rownumbers;
	}


	public void setRownumbers(String rownumbers) {
		this.rownumbers = rownumbers;
	}


	public String getSingleSelect() {
		return singleSelect;
	}


	public void setSingleSelect(String singleSelect) {
		this.singleSelect = singleSelect;
	}


	public String getCheckOnSelect() {
		return checkOnSelect;
	}


	public void setCheckOnSelect(String checkOnSelect) {
		this.checkOnSelect = checkOnSelect;
	}


	public String getSelectOnCheck() {
		return selectOnCheck;
	}


	public void setSelectOnCheck(String selectOnCheck) {
		this.selectOnCheck = selectOnCheck;
	}


	public String getPagePosition() {
		return pagePosition;
	}


	public void setPagePosition(String pagePosition) {
		this.pagePosition = pagePosition;
	}


	public String getPageNumber() {
		return pageNumber;
	}


	public void setPageNumber(String pageNumber) {
		this.pageNumber = pageNumber;
	}


	public String getPageSize() {
		return pageSize;
	}


	public void setPageSize(String pageSize) {
		this.pageSize = pageSize;
	}


	public String getPageList() {
		return pageList;
	}


	public void setPageList(String pageList) {
		this.pageList = pageList;
	}


	public String getQueryParams() {
		return queryParams;
	}


	public void setQueryParams(String queryParams) {
		this.queryParams = queryParams;
	}


	public String getSortName() {
		return sortName;
	}


	public void setSortName(String sortName) {
		this.sortName = sortName;
	}


	public String getSortOrder() {
		return sortOrder;
	}


	public void setSortOrder(String sortOrder) {
		this.sortOrder = sortOrder;
	}


	public String getRemoteSort() {
		return remoteSort;
	}


	public void setRemoteSort(String remoteSort) {
		this.remoteSort = remoteSort;
	}


	public String getShowFooter() {
		return showFooter;
	}


	public void setShowFooter(String showFooter) {
		this.showFooter = showFooter;
	}


	public String getRowStyler() {
		return rowStyler;
	}


	public void setRowStyler(String rowStyler) {
		this.rowStyler = rowStyler;
	}


	public String getLoadFilter() {
		return loadFilter;
	}


	public void setLoadFilter(String loadFilter) {
		this.loadFilter = loadFilter;
	}


	public String getOnLoadSuccess() {
		return onLoadSuccess;
	}


	public void setOnLoadSuccess(String onLoadSuccess) {
		this.onLoadSuccess = onLoadSuccess;
	}


	public String getOnLoadError() {
		return onLoadError;
	}


	public void setOnLoadError(String onLoadError) {
		this.onLoadError = onLoadError;
	}


	public String getOnBeforeLoad() {
		return onBeforeLoad;
	}


	public void setOnBeforeLoad(String onBeforeLoad) {
		this.onBeforeLoad = onBeforeLoad;
	}


	public String getOnClickRow() {
		return onClickRow;
	}


	public void setOnClickRow(String onClickRow) {
		this.onClickRow = onClickRow;
	}


	public String getOnDblClickRow() {
		return onDblClickRow;
	}


	public void setOnDblClickRow(String onDblClickRow) {
		this.onDblClickRow = onDblClickRow;
	}


	public String getOnClickCell() {
		return onClickCell;
	}


	public void setOnClickCell(String onClickCell) {
		this.onClickCell = onClickCell;
	}


	public String getOnDblClickCell() {
		return onDblClickCell;
	}


	public void setOnDblClickCell(String onDblClickCell) {
		this.onDblClickCell = onDblClickCell;
	}


	public String getOnSortColumn() {
		return onSortColumn;
	}


	public void setOnSortColumn(String onSortColumn) {
		this.onSortColumn = onSortColumn;
	}


	public String getOnResizeColumn() {
		return onResizeColumn;
	}


	public void setOnResizeColumn(String onResizeColumn) {
		this.onResizeColumn = onResizeColumn;
	}


	public String getOnSelect() {
		return onSelect;
	}


	public void setOnSelect(String onSelect) {
		this.onSelect = onSelect;
	}


	public String getOnUnselect() {
		return onUnselect;
	}


	public void setOnUnselect(String onUnselect) {
		this.onUnselect = onUnselect;
	}


	public String getOnSelectAll() {
		return onSelectAll;
	}


	public void setOnSelectAll(String onSelectAll) {
		this.onSelectAll = onSelectAll;
	}


	public String getOnUnselectAll() {
		return onUnselectAll;
	}


	public void setOnUnselectAll(String onUnselectAll) {
		this.onUnselectAll = onUnselectAll;
	}


	public String getOnCheck() {
		return onCheck;
	}


	public void setOnCheck(String onCheck) {
		this.onCheck = onCheck;
	}


	public String getOnUncheck() {
		return onUncheck;
	}


	public void setOnUncheck(String onUncheck) {
		this.onUncheck = onUncheck;
	}


	public String getOnCheckAll() {
		return onCheckAll;
	}


	public void setOnCheckAll(String onCheckAll) {
		this.onCheckAll = onCheckAll;
	}


	public String getOnUncheckAll() {
		return onUncheckAll;
	}


	public void setOnUncheckAll(String onUncheckAll) {
		this.onUncheckAll = onUncheckAll;
	}


	public String getOnBeforeEdit() {
		return onBeforeEdit;
	}


	public void setOnBeforeEdit(String onBeforeEdit) {
		this.onBeforeEdit = onBeforeEdit;
	}


	public String getOnAfterEdit() {
		return onAfterEdit;
	}


	public void setOnAfterEdit(String onAfterEdit) {
		this.onAfterEdit = onAfterEdit;
	}


	public String getOnCancelEdit() {
		return onCancelEdit;
	}


	public void setOnCancelEdit(String onCancelEdit) {
		this.onCancelEdit = onCancelEdit;
	}


	public String getOnHeaderContextMenu() {
		return onHeaderContextMenu;
	}


	public void setOnHeaderContextMenu(String onHeaderContextMenu) {
		this.onHeaderContextMenu = onHeaderContextMenu;
	}


	public String getOnRowContextMenu() {
		return onRowContextMenu;
	}


	public void setOnRowContextMenu(String onRowContextMenu) {
		this.onRowContextMenu = onRowContextMenu;
	}


	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public String getHeight() {
		return height;
	}

	public void setHeight(String height) {
		this.height = height;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public SQLTag getSqltag() {
		return sqltag;
	}

	public void setSqltag(SQLTag sqltag) {
		this.sqltag = sqltag;
	}

	public String getView() {
		return view;
	}

	public void setView(String view) {
		this.view = view;
	}

	public ExportTag getExport() {
		return export;
	}

	public void setExport(ExportTag export) {
		this.export = export;
	}

	public List<ColumnTag> getCheckBoxColumns() {
		return checkBoxColumns;
	}

	public void setCheckBoxColumns(List<ColumnTag> checkBoxColumns) {
		this.checkBoxColumns = checkBoxColumns;
	}

	

	public String getWidth() {
		return width;
	}

	public void setWidth(String width) {
		this.width = width;
	}

	public SearchBoxTag getSearch() {
		return search;
	}

	public void setSearch(SearchBoxTag search) {
		this.search = search;
	}

	public List<ColumnTag> getFrozenColumns() {
		return frozenColumns;
	}

	public void setFrozenColumns(List<ColumnTag> frozenColumns) {
		this.frozenColumns = frozenColumns;
	}

	public EasyTagContainer getContainer() {
		return container;
	}

	public Boolean getAutotoolbar() {
		return autotoolbar;
	}

	public void setAutotoolbar(Boolean autotoolbar) {
		this.autotoolbar = autotoolbar;
	}

	

	

	
	


	
	





}
